import java.io.IOException;
import java.util.ArrayList;

import optimizer.BasicBlock;
import optimizer.DependencyTable;
import optimizer.Instruction;
import formatter.AssemblyFileReader;
import formatter.AssemblyFileWriter;


public class testtwo {

	public static void main(String[] args) {
		System.out.println("Test2 starting...");
		ArrayList<Instruction> instructions = new ArrayList<Instruction>();

		AssemblyFileReader afr = new AssemblyFileReader("program.s");
		Instruction ins=null;
		do {
			try {
				ins = afr.readInstruction();
				if(ins != null)
					instructions.add(ins);
			} catch (IOException e) {
			}
		}while(ins != null);
		afr.closeFile();

		BasicBlock block = new BasicBlock(instructions);
		DependencyTable unopttable = DependencyTable.buildDependencyTable(block);

		AssemblyFileWriter unoptimizedtable = new AssemblyFileWriter("unoptimized.tbl");
		try {
			unoptimizedtable.writeDependencyTable(unopttable);
		} catch (IOException e) {
			System.out.println(">>>>> ERROR 1");
		}
		unoptimizedtable.closeFile();


		DependencyTable opttable = unopttable.optimize();

		if(opttable != null) {
			AssemblyFileWriter optimizedtable = new AssemblyFileWriter("optimized.tbl");
			try {
				optimizedtable.writeDependencyTable(opttable);
			} catch (IOException e) {
				System.out.println(">>>>> ERROR 2");
			}
			optimizedtable.closeFile();


			AssemblyFileWriter optimizedprogram = new AssemblyFileWriter("optimized.s");
			try {
				optimizedprogram.writeProgram(opttable);
			} catch (IOException e) {
				System.out.println(">>>>> ERROR 3");
			}
			optimizedprogram.closeFile();

		}

		System.out.println("Test2 finished.");
	}
}
